package org.dfl.core.utils;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.Iterator;

import javax.imageio.ImageIO;
import javax.imageio.ImageReader;
import javax.imageio.stream.ImageInputStream;

/**
 * class to manage images 
 * @author Piotr Wlodarczyk
 *
 */
public class ImageUtils {
	// Returns the format of the image in the file 'f'.
    // Returns null if the format is not known.
    public static String getFormatInFile(File f) {
        return getFormatName(f);
    }
    
    // Returns the format of the image in the input stream 'is'.
    // Returns null if the format is not known.
    public static String getFormatFromStream(InputStream is) {
        return getFormatName(is);
    }
    
    // Returns the format of the image in the input stream 'is'.
    // Returns null if the format is not known.
    public static String getFormatFromStream(byte[] is) {
    	ByteArrayInputStream dat = new ByteArrayInputStream(is);
    	String res = getFormatName(dat);
    	try {
			dat.close();
		} catch (IOException e) {
		}
        return res;
    }
    
	// Returns the format name of the image in the object 'o'.
    // 'o' can be either a File or InputStream object.
    // Returns null if the format is not known.
    @SuppressWarnings("unchecked")
	private static String getFormatName(Object o) {
    	ImageInputStream iis = null;
        try {
            // Create an image input stream on the image
            iis = ImageIO.createImageInputStream(o);
    
            // Find all image readers that recognize the image format
            Iterator iter = ImageIO.getImageReaders(iis);
            if (!iter.hasNext()) {
                // No readers found
                return null;
            }
    
            // Use the first reader
            ImageReader reader = (ImageReader)iter.next();
    
            // Return the format name
            return reader.getFormatName();
        } catch (IOException e) {
        }finally{
        	if(iis!=null){
        		try {
                    // Close stream
                    iis.close();
				} catch (IOException e1) {}
        	}
        }
        // The image could not be read
        return null;
    }
}
